package com.zhangguo.springmvc01.controller;

import com.zhangguo.springmvc01.entities.Person;
import com.zhangguo.springmvc01.entities.Product;
import com.zhangguo.springmvc01.entities.ProductType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangxing
 * @version 2020/5/30 7:28 Administrator
 */
//BarController类的实例是一个控制器，会自动添加到Spring上下文中
@Controller
@RequestMapping("/bar")
public class BarController {
    ////映射访问路径
    @RequestMapping("/")
    public String index(Model model){
        //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("message","这是通过注解定义的一个控制器中的Action");
        //返回视图位置
        return "foo/index";
    }

    @RequestMapping("/action1")
    public String action1(Model model){
        model.addAttribute("message","RequestMapping 注释方法不注释类目录");
        return "foo/index";
    }
    @RequestMapping("/action11")
    public String action11(Model model){
        //向模型中添加一个名为product的对象，用于渲染视图
        model.addAttribute("product", new Product(1,"Meizu note1", 999));
        model.addAttribute("product", new Product());
        return "bar/action11";
    }
    @RequestMapping("/action21")
    public String action21(Model model){
        model.addAttribute("person", new Person());
        return "bar/action21";
    }

    @RequestMapping("/action22")
    @ResponseBody
    public Person action22(HttpServletResponse response, Person person){
        return person;
    }

    @RequestMapping("/action31")
    public String action31(Model model){
        model.addAttribute("person", new Person());
        return "bar/action31";
    }

    @RequestMapping("/action32")
    @ResponseBody
    public Person action32(HttpServletResponse response,Person person){
        return person;
    }
    @RequestMapping("/action33")
    public String action33(Model model){
        Person person=new Person();
        person.setEducation("edu");
        model.addAttribute("person", person);
        return "bar/action31";
    }
    //select 下拉列表
    @RequestMapping("/action41")
    public String action41(Model model){
        List<ProductType>  productTypes = getProductTypeList();
        model.addAttribute("productTypes", productTypes);
        model.addAttribute("person", new Person());
        return "bar/action41";
    }

    @RequestMapping("/action42")
    @ResponseBody
    public Person action42(HttpServletResponse response,Person person){
        return person;
    }

    //option
    @RequestMapping("/action51")
    public String action51(Model model){
        model.addAttribute("person", new Person());
        return "bar/action51";
    }

    @RequestMapping("/action52")
    @ResponseBody
    public Person action52(HttpServletResponse response,Person person){
        return person;
    }
    public List<ProductType> getProductTypeList(){
        List<ProductType>  productTypes = new ArrayList<ProductType>();
        productTypes.add(new ProductType(11, "数码电子"));
        productTypes.add(new ProductType(21, "鞋帽服饰"));
        productTypes.add(new ProductType(31, "图书音像"));
        productTypes.add(new ProductType(41, "五金家电"));
        productTypes.add(new ProductType(51, "生鲜水果"));
        return productTypes;
    }
    //options
    @RequestMapping("/action61")
    public String action61(Model model){
        List<ProductType>  productTypes = getProductTypeList();
        model.addAttribute("productTypes", productTypes);
        model.addAttribute("person", new Person());
        return "bar/action61";
    }

    @RequestMapping("/action62")
    @ResponseBody
    public Person action62(HttpServletResponse response,Person person){
        return person;
    }

    //hidden
    @RequestMapping("/action71")
    public String action71(Model model){
        Person person=new Person();
        person.setEducation("9");
        model.addAttribute("person", person);
        return "bar/action71";
    }

    @RequestMapping("/action72")
    @ResponseBody
    public Person action72(HttpServletResponse response,Person person){
        return person;
    }

    //radiobuttons,checkboxs
    @RequestMapping("/action81")
    public String action81(Model model) {
        List<ProductType> productTypes = getProductTypeList();
        model.addAttribute("productTypes", productTypes);
        model.addAttribute("person", new Person());
        return "bar/action81";
    }

    @RequestMapping("/action82")
    @ResponseBody
    public Person action82(HttpServletResponse response, Person person) {
        return person;
    }
    @RequestMapping("/index")
    public String index() {
        return "index";
    }
}
